package ink.lovejinhu.leetcode;

import java.util.HashMap;

/**
 * @author jinhu
 * created 2021-07-14 15:47
 */
public class Solution_0405 {
    public static void main(String[] args) {
        Solution_0405 solution_0405 = new Solution_0405();
        String s = solution_0405.toHex(-2);
        System.out.println(s);
    }

    /**
     * 转化为16进制
     *
     * @param num
     * @return
     */
    public String toHex(int num) {
        HashMap<Integer, Character> map = new HashMap<Integer, Character>();
        map.put(0, '0');
        map.put(1, '1');
        map.put(2, '2');
        map.put(3, '3');
        map.put(4, '4');
        map.put(5, '5');
        map.put(6, '6');
        map.put(7, '7');
        map.put(8, '8');
        map.put(9, '9');
        map.put(10, 'a');
        map.put(11, 'b');
        map.put(12, 'c');
        map.put(13, 'd');
        map.put(14, 'e');
        map.put(15, 'f');

        if (num > 0) {
            StringBuilder res = new StringBuilder();
            int[] yuanMa = this.getYuanMa(num);
            int[] ints = this.changeBinToHex(yuanMa);
            int[] ints1 = this.deleteZero(ints);
            for (int i = 0; i < ints1.length; i++) {

                res.append(map.get(ints1[i]));


            }
            return res.toString();
        } else if (num == 0) {
            return "0";
        } else {
            if (num == Integer.MIN_VALUE) return "80000000";
            StringBuilder res = new StringBuilder();
            int[] yuanMa = this.getYuanMa(-num);
            int flag = 1;
            for (int i = yuanMa.length - 1; i >= 0; i--) {
                if (yuanMa[i] == 0) {
                    yuanMa[i] = 1;
                } else {
                    yuanMa[i] = 0;
                }

                int temp = (yuanMa[i] + flag) % 2;
                flag = (yuanMa[i] + flag) / 2;
                yuanMa[i] = temp;
            }
            int[] ints = this.changeBinToHex(yuanMa);
            for (int i = 0; i < ints.length; i++) {
                res.append(map.get(ints[i]));
            }
            return res.toString();

        }

    }

    public int[] getYuanMa(int num) {
        int[] ints = new int[32];
        int flag = 31;
        while (num > 0) {
            ints[flag] = num % 2;
            num = num / 2;
            flag--;
        }
        return ints;
    }

    public int[] changeBinToHex(int[] bin) {
        int[] ints = new int[8];
        int flag = ints.length - 1;
        for (int i = bin.length - 1; i >= 0; i = i - 4) {
            ints[flag] = bin[i] + bin[i - 1] * 2 + bin[i - 2] * 2 * 2 + bin[i - 3] * 2 * 2 * 2;
            flag--;
        }
        return ints;
    }

    public int[] deleteZero(int[] ints) {
        int flag = 0;
        for (; flag < ints.length; ) {
            if (ints[flag] != 0) break;
            flag++;
        }
        int[] ints1 = new int[ints.length - 1 - flag + 1];
        for (int i = 0; i < ints1.length; i++) {
            ints1[i] = ints[i + flag];

        }
        return ints1;
    }
}
